package cn.fx.framework.spring;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * @author ljh
 * @since 2018-11-10 12:53
 **/
public class SpringBeanUtils {
    /**
     * 获取SpringBootApplication上的注解类A
     * @param context
     * @param a
     * @return
     * @param <A>
     */
    public static <A extends Annotation> A getAnnotationAtSpringBootApplication(ApplicationContext context, Class<A> a){
        Map<String, Object> beans = context.getBeansWithAnnotation(a);
        for(Map.Entry<String,Object> entry : beans.entrySet()) {
            Object bean = entry.getValue();
            SpringBootApplication sba = bean.getClass().getAnnotation(SpringBootApplication.class);
            if (sba != null) {
                return bean.getClass().getAnnotation(a);
            }
        }
        return null;
    }
}
